import mockData from './mockdata.js';
import axios from 'axios';

const isMock = false
const urlServe = 'https://admin.santoker.com/apis/user-service/admin/painters/apis/'
// const urlServe = 'http://localhost:8198/user-service/admin/painters/apis/'

function POST(mock, url, param, method, successCallback, errCallback) {
    if (mock) {
        postMock(url, param, method, successCallback);
    } else {
        postHttp(url, param, method, successCallback);
    }
}

function postHttp(url, param, method, successCallback) {
    if (url == undefined | url == "") {
        console.error('No available url of http for this operation!');
    }
    let relurl = urlServe + url;
    let params = {};
    if (method == 'get') {
        params = {
            method: method,
            url: relurl,
            params: param,
            header: {'Content-Type': 'application/json'}
        }
    } else if (method == 'post') {
        params = {
            method: method,
            url: relurl,
            data: param,
            header: {'Content-Type': 'application/json'}
        }
    }

    axios(params).then(result => {
        // Indicator.close();
        successCallback(result.data);
    }).catch(error => {
        if (error.response) {
            ////console.log('err response', JSON.stringify(error.response));
            let errData = error.response.data.data;
            vueRouter.replace({
                name: "error",
                params: {errCode: errData.mbank_errorCode, errMsg: errData.mbank_errorMessage}
            });
        }
        // Indicator.close();
    });
}


function postMock(url, param, header, func_succ) {
    console.log('get mock data ： ' + url);
    console.log(param);
    let res = mockData.getMockData(url);
    console.log('get mock data result： ' + JSON.stringify(res));
    func_succ(res);
}


export default POST;
